﻿using System;
using System.Configuration;
using System.Globalization;
using System.Security.Cryptography;
using Ipmce.RemoteVoting.Backend.DataAccess;
using Ipmce.RemoteVoting.Common.Tools.CardReader;

namespace Ipmce.RemoteVoting.Tools.VoterCreator
{
	class Program
	{
		static void Main(string[] args)
		{
			if (args.Length != 4)
			{
				Console.WriteLine("Usage: fio passportNumber bithday mrz");
				return;
			}
		    var fio = args[0];
			var passportNumber = args[1];
		    var birthday = DateTime.Parse(args[2], new CultureInfo("ru-ru"));
			var mrz = args[3];
            using (var context = new RemoteVotingContext(ConfigurationManager.AppSettings["RemoteVotingDataBase"]))
		    {
                var voter = new Voter
                                {
                                    Fio = fio,
                                    BirthDate = birthday,
                                    PassportNumber = passportNumber
                                };
                var hasher = new SHA1CryptoServiceProvider();
		        var passportReader = new PassportReaderFacade();
                voter.Dg2Hash = hasher.ComputeHash(passportReader.GetRawDg2Data(mrz));
		        context.Voters.Add(voter);
                context.SaveChanges();
            }
		}
	}
}
